var t = getApp(),
    a = t.requirejs("core"),
    e = t.requirejs("jquery"),
    i = t.requirejs("foxui");

Page({
    data: {
        imgs: [], //本地图片地址数组
        picPaths: [], //网络路径
    },
    onLoad: function () {
        this.getData();
    },
    onShow: function () {
        /*  */
    },
    //logo添加上传图片
    chooseImageTap: function () {
        var that = this;
        wx.showActionSheet({
            itemList: ['从相册中选择', '拍照'],
            itemColor: "#00000",
            success: function (res) {
                if (!res.cancel) {
                    if (res.tapIndex == 0) {
                        that.chooseWxImage('album')
                    } else if (res.tapIndex == 1) {
                        that.chooseWxImage('camera')
                    }
                }
            }
        })
    },
    // logo图片本地路径
    chooseWxImage(type) {
        var that = this;
        var imgsPaths = that.data.imgs;
        wx.chooseMedia({
            mediaType: ['image'],
            sourceType: [type],
            count: 1,
            success: function (res) {
                that.upImgs(res.tempFiles[0].tempFilePath,'logo')
            }
        })
    },
    //营业执照添加上传图片
    chooseImageTapa: function () {
        var that = this;
        wx.showActionSheet({
            itemList: ['从相册中选择', '拍照'],
            itemColor: "#00000",
            success: function (res) {
                if (!res.cancel) {
                    if (res.tapIndex == 0) {
                        that.chooseWxImagea('album')
                    } else if (res.tapIndex == 1) {
                        that.chooseWxImagea('camera')
                    }
                }
            }
        })
    },
    // 营业执照图片本地路径
    chooseWxImagea: function (type) {
        var that = this;
        var imgsPaths = that.data.imgs;
        wx.chooseMedia({
            mediaType: ['image'],
            sourceType: [type],
            count: 1,
            success: function (res) {
                that.upImgs(res.tempFiles[0].tempFilePath, 'yinye')
            }
        })
    },
    //上传服务器
    upImgs: function (imgurl,type) {
     var that = this;
     wx.uploadFile({
      url: `${getApp().globalData.api}&r=member.upload2`,//
      filePath: imgurl,
      name: 'file',
     /*  header: {
       'content-type': 'multipart/form-data'
      }, */
      success: function (res) {
          console.log(res.data,159955);
          let data=JSON.parse(res.data)
          if(data.result.status=="success"){
              if(type=='logo'){
                that.setData({
                    src_logo: data.result.url
                })
              }else if(type=='yinye'){
                that.setData({
                     src_yingye: data.result.url
                 })
              }
          }
       
      }
     })
    },
    getData: function () {
        var t = this;
        a.get("changce/merch/apply", {}, function (e) {
            if( 1 == e.canapply){
                e.show = !0, t.setData(e)
                t.setData({
                    src_logo:t.data.reg.logo,
                    src_yingye:t.data.reg.zz,
                })
            }else{
                a.alert(e.message)
            }
           /*  1 == e.canapply ? (e.show = !0, t.setData(e)) : a.alert(e.message); */
        }, !1, !0);
    },
    typeChange: function (t) {
        var a = t.detail.value,
            e = this.data.type_array[a].type;
        this.setData({
            applytype: e,
            applyIndex: a
        });
    },
    bankChange: function (t) {
        var a = t.detail.value;
        this.setData({
            bankIndex: a
        });
    },
    inputChange: function (t) {
        var a = this.data.reg,
            i = t.currentTarget.dataset.type,
            s = e.trim(t.detail.value);
        a[i] = s, this.setData({
            reg: a
        });
    },
    submit: function (t) {
        var e = this,
            s = this.data;
        console.log(s, 66);
        if (s.canapply && !s.isSubmit) {
            if (!s.reg.merchname) return void i.toast(e, "请填写商户名称");
            if (!s.reg.salecate) return void i.toast(e, "请填写主营项目");
            if (!s.reg.realname) return void i.toast(e, "请填写联系人");
            if (!s.reg.mobile) return void i.toast(e, "请填写手机号");
            if (!s.src_yingye) return void i.toast(e, "请导入营业执照图片");
            if (!s.src_logo) return void i.toast(e, "请导入logo图片");
            /* if (!s.reg.uname) return void i.toast(e, "请填写账号");
            if (!s.reg.upass) return void i.toast(e, "请填写密码"); */
            var merchname = s.reg.merchname
            var salecate = s.reg.salecate
            var uname = s.reg.realname
            var upass = s.reg.mobile
            var zz = s.src_yingye
            var logo = s.src_logo
            a.confirm("确认要提交申请吗？", function () {
                e.setData({
                    isSubmit: !0
                }), a.post("changce/merch/apply", {
                    merchname,
                    salecate,
                    uname,
                    upass,
                    zz,
                    logo
                }, function (t) {
                    console.log(t, 33);
                    if (1 != t.status) return i.toast(e, t.result.message), void e.setData({
                        isSubmit: !1
                    });
                    i.toast(e, "申请成功，请等待审核！"), setTimeout(function () {
                        wx.navigateBack();
                    }, 2e3);
                }, !0, !0);
            });
        }
    },
    confirmjoin: function (t) {
        var e = this,
            s = this.data;
        s.isSubmit || (e.setData({
            isSubmit: !0
        }), a.post("changce/merch/confirmjoin", s.reg, function (t) {
            if (1 != t.status) return i.toast(e, t.result.message), void e.setData({
                isSubmit: !1
            });
            i.toast(e, "入驻成功！"), setTimeout(function () {
                wx.navigateBack();
            }, 2e3);
        }, !0, !0));
    }
});